package com.automapper.utils;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

public class MetaObjectUtils {
	
	private MetaObjectUtils(){}

	public static MetaObject getOriginalMetaObject(MetaObject metaExecutor) {
		MetaObject executor = metaExecutor;
		// 分离代理对象链(由于目标类可能被多个拦截器拦截，从而形成多次代理，通过下面的两次循环可以分离出最原始的的目标类)
		while (executor.hasGetter("h")) {
			Object object = executor.getValue("h");
			executor = SystemMetaObject.forObject(object);

			// 分离代理对象的目标类
			while (executor.hasGetter("target")) {
				object = executor.getValue("target");
				executor = SystemMetaObject.forObject(object);
			}
		}
		return executor;
	}
}
